﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DunLibrary.Dun;
using DunSilverlight.Comm;
using DunLibrary.CreditCard;
using Telerik.Windows.Controls.MaskedInput;
using Telerik.Windows.Controls;
using System.Windows.Data;

namespace DunSilverlight.Views
{
    public partial class CallRecordForm : UserControl
    {
        private CallRecord callRecord;
        private IList<CardholderContact> contacts;

        public Action<CallRecord> SaveCallBack;

        public CallRecordForm(CallRecord callRecord,IList<CardholderContact> contacts)
        {
            this.callRecord = callRecord;
            this.contacts = contacts;
            
            InitializeComponent();
            Loaded += new RoutedEventHandler(CallRecordForm_Loaded);

           
        }

        void CallRecordForm_Loaded(object sender, RoutedEventArgs e)
        {
            LoadReciveTypes();
            List<CardholderContact> ls = new List<CardholderContact>();
            foreach (var item in contacts.Where(p => p.ContactType.Name == "电话"))
            {
                string[] s;
                if (string.IsNullOrEmpty(item.Name))
                    s = new string[] { item.Content, item.Description };
                else
                    s = new string[] { item.Content, item.Description, item.Name };
                ls.Add(new CardholderContact { Content = string.Join(",", s) });
            }
            cmbContact.ItemsSource = ls;

            DataContext = callRecord;
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (!CheckVerify())
                return;
            try
            {
                CallRecordService.CallRecordServiceClient ser = new CallRecordService.CallRecordServiceClient();
                ser.SaveOrUpdateCompleted += (object sender1, CallRecordService.SaveOrUpdateCompletedEventArgs e1) =>
                    {
                        int id = JsonSerializerHelper.JsonToEntity<int>(e1.Result);

                        if (id > 0)
                        {
                            callRecord.Id = id;
                            if(SaveCallBack!=null)
                                SaveCallBack(callRecord);
                        }
                    };

                string json = Newtonsoft.Json.JsonConvert.SerializeObject(callRecord);
                ser.SaveOrUpdateAsync(json);
            }
            catch (Exception ex)
            {
                Tools.ShowMessage(ex.Message, "", false);
            }
        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            (this.Parent as RadWindow).Close();
        }

        private bool CheckVerify()
        {
            if (string.IsNullOrEmpty(callRecord.Contact))
            {
                Tools.ShowMessage("请选择电话号码!", "", false);
                return false;
            }

            if (callRecord.ReceiveType == null)
            {
                Tools.ShowMessage("请选择联系结果!", "", false);
                return false;
            }

            return true;
        }

        private void LoadReciveTypes()
        {
            Action<IList<ReceiveTypeOfCall>> callback = (IList<ReceiveTypeOfCall> types) =>
            {
                cmbReciveType.ItemsSource = types;

                object o = callRecord.ReceiveType;
                
                //cmbReciveType.SelectedValue = callRecord.ReceiveType;

                
                
            };
            AppGlobal.GetReceiveTypeCalls(callback);
        }
        
    }

   
}
